<?php

namespace App\Repository;

use App\Entity\Money;
use App\Entity\MoneyLog;
use App\Entity\MoneyType;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
 * @method Money|null find($id, $lockMode = null, $lockVersion = null)
 * @method Money|null findOneBy(array $criteria, array $orderBy = null)
 * @method Money[]    findAll()
 * @method Money[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class MoneyRepository extends ServiceEntityRepository
{

    public $userRepository;
//    public $moneyTypeRepository;
//    public $moneyLogRepository;
//    public $moneyConfigRepository;
//    public $moneyStaticRepository;
//    public $moneyLogTypeRepository;

    public function __construct(
        ManagerRegistry $registry
//        ,UserRepository $userRepository
//        ,MoneyLogRepository $moneyLogRepository
//        ,MoneyTypeRepository $moneyTypeRepository
//        ,MoneyConfigRepository $moneyConfigRepository
//        ,MoneyStaticRepository $moneyStaticRepository
//        ,MoneyLogTypeRepository $moneyLogTypeRepository
    )
    {
        parent::__construct($registry, Money::class);
//        $this->userRepository = $userRepository;
//        $this->moneyLogRepository = $moneyLogRepository;
//        $this->moneyTypeRepository = $moneyTypeRepository;
//        $this->moneyConfigRepository = $moneyConfigRepository;
//        $this->moneyStaticRepository = $moneyStaticRepository;
//        $this->moneyLogTypeRepository = $moneyLogTypeRepository;
    }



    /**
     * @param User $user
     * @param MoneyType $moneyType
     * @return Money|null
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function getUserMoneyByType(User $user,MoneyType $moneyType)
    {
        $money = $this->findOneBy(array("type"=>$moneyType));
        if (!$money) {
            $money = new Money();
            $money->setUser($user)
                ->setType($moneyType)
                ->setMoney(0);
            $this->_em->persist($moneyType);
            $this->_em->flush();
        }
        return $money;
    }
}
